<?php
/**
 * Roles Management
 *
 */
class Admin_RolesController extends Zend_Controller_Action
{
    function init()
    {
        $this->Params = $this->getRequest()->getParams();
        System_Template::using("getit","Admin");
        $this->model = new Admin_Model_Roles();
        parent::init();
    }
	
	/**
    * List Roles
    */
    function indexAction()
    {
        $rs = $this->model->selectAll();
        $sD = config::getSourcesDescription();
        $data = $sD->module->toArray();
        
        $this->view->data = $rs ;
        $this->view->dataSources = $data; 
    }
	
	/**
    * INVISIBLE
	* Redirect to add or edit form
    */
    function inputAction()
    {
        /* Kiểm tra đó là add mới role hay chỉnh sửa 1 role có sẵn */
        if(isset($this->Params['id'])){ 
			// chỉnh sửa role
			$ROLEID = $this->Params['id'];
            $select = $this->model->select()
								->from($this->model,array('ROLEID','NAME','DESCRIPTION','STATUS','NAVIGATION'))
                                ->where('ROLEID = ?',$ROLEID);
            $data_row = $this->model->fetchAll($select);
            $this->view->data = $data_row->current();
        } else {
			// add mới role
			$this->view->roleID = ''; 
		}
		/* ************************************** */
		
		/* Lấy dữ liệu về các trang view trong các module để truyền vào combobox NAVIGATION */
		
		$sD = config::getSourcesDescription();
        $dataSources = $sD->module->toArray();
        $this->view->dataSources = $dataSources; 
		
		/* ************************************** */
    }
    
	/**
    * INVISIBLE
	* Save a new Role or edit an existed Role
    */
	
	function saveAction()
    {
        if($this->Params['STATUS']=='on'){
			$this -> Params ['STATUS'] = '1';
		} else {
			$this -> Params ['STATUS'] = '0';
		}
		
        if($this->Params['NAME'] != ''){
			if($this->Params['ROLEID'] != '' ){
				$Params = array(
					'ROLEID' => $this->Params['ROLEID'],
					'NAME' => $this->Params['NAME'],
					'DESCRIPTION' => $this->Params['DESCRIPTION'],
					'STATUS' => $this->Params['STATUS'],
					'NAVIGATION' => $this->Params['NAVIGATION']
				);
				
				$this->model->updateRole($Params);
			} else {
				
				$roleID = $this->model->addNewRole($this->Params);
				if($roleID != 0) {
					config::addSystemRoles($roleID);
				}
			}
        }
        
		$this->_redirect('/admin/roles/index');
    }
    
    /**
    * INVISIBLE
	* Change default page after login of this Role
    */
    function changenavigationAction()
    {
        try{
            $Params = array( 
					'ROLEID' => $this->Params['ROLEID'],
					'NAVIGATION' => $this->Params['URLNAV']
					);
            
            $this->model->updateRole($Params);
            ajax::ship('rsChangeNav',1);
        }catch(Exception $ex ){
            ajax::ship('rsChangeNav',0);
            ajax::ship('rsChangeNavError',$ex->getMessage());
        }
        exit;
    }
	
	/**
    * INVISIBLE
	* Change status of this Role
    */
    function changestatusAction() {
        
		
		$Params = array( 
					'ROLEID' => $this->Params['itemID'],
					'STATUS' => $this->Params['status']
					);
        $this->model->updateRole($Params);
        exit;
    }
	
	/**
    * INVISIBLE
	* Change status of selected Roles
    */
	function changestatusmultiitemsAction(){
		$ids = $this -> Params['itemIDs'];
		$status = $this -> Params['status'];
        foreach($ids as $t){
			$Params = array( 
					'ROLEID' => $t,
					'STATUS' => $status
					);
			$this->model->updateRole($Params);
        }
		exit;
	}
	
	/**
    * INVISIBLE
	* Delete a Role
    */
	function deleteAction() {
        $itemID = $this -> Params['id'];
		if((int)$itemID < 3) {
			$this->_redirect("/admin/roles/index");
			exit;
		}
		// Logging class initialization
        $this->model->deleteRole($itemID);
        
        $this->_redirect("/admin/roles/index");
    }
	
	/**
    * INVISIBLE
	* Delete selected Roles
    */
	function deletemultiitemAction(){
		$ids = $this -> Params['ITEM'];
        foreach($ids as $t){
            if((int)$t>2){
				$this->model->deleteRole("role","ROLEID = ".(int)$t);
			}
        }
        $this->_redirect("/admin/roles/index");
	}
}